package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ParamsErrorException;

import com.indusfo.spc.mapper.ProStationMapper;

import com.indusfo.spc.pojo.ProStation;
import com.indusfo.spc.pojo.Station;
import com.indusfo.spc.service.ProStationService;
import com.indusfo.spc.vo.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * 工序业务层
 *
 * @author xuz
 * @date 2018/11/23 2:30 PM
 */
@Service
public class ProStationServiceImpl implements ProStationService {

    private  static  final Logger logger = LoggerFactory.getLogger(ProStationServiceImpl.class);

    @Resource
    private ProStationMapper proStationMapper;



    @Override
    public JSONObject queryProList(ProStation proList) {
        List<ProStation> listProList = null;
        try {
            Integer pagesize = proList.getPagesize();
            Integer pageindex = proList.getPageindex();
            if(pagesize != null && pageindex != null) {
                proList.setIncept(pagesize*(pageindex - 1));
            }
            listProList = proStationMapper.queryProList(proList);

            if (listProList.isEmpty()) {
                return JSONObject.oK("没有相关工序数据", listProList, 0);
            }
            //查询分页总数
            int count = proStationMapper.countProList(proList);
            return JSONObject.oK("查询成功", listProList, count);
        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    @Override
    public JSONObject queryFilterStation(Station station,Integer[] stationIds) {
        if(station.getProductlineId()==null || "".equals(station.getProductlineId())){
            return JSONObject.oK("请选择产线");
        }
        Integer pagesize = station.getPagesize();
        Integer pageindex = station.getPageindex();
        if(pagesize != null && pageindex != null) {
            station.setIncept(pagesize*(pageindex - 1));
        }
        List<Integer> querySationList=new ArrayList<>();
        if(stationIds!=null && stationIds.length>0){
            for (Integer stationId : stationIds) {
                querySationList.add(stationId);
            }
        }

        List<Station> stations = proStationMapper.queryFilterStation(station,querySationList);
        if(!stations.isEmpty()){
            int countFilterStationNum = proStationMapper.countFilterStation(station, querySationList);
            return JSONObject.oK("查询成功",stations,countFilterStationNum);
        }
        return JSONObject.oK("暂无相关数据",stations,0);
    }


}
